/***********************************************
 **** 版权声明处 **
 ****  为了方便阅读和维护，请严格遵守相关代码规范，谢谢   ****
 *******************************************/
package com.hnisi.decorators
{
	import com.hnisi.Graph;
	import com.hnisi.events.GraphEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/*******************************************
	 **** @author huangjixin,@date 2012-6-21, @time 下午2:39:04 **
	 **** 该类为边饰管理器，用于管理拖拽矩形框绘制和实现自动对齐功能  **
	 *******************************************/
	public class DecoratorManager
	{
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 类私有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：private static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 类公有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：public static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 私有变量声明处，请以“_”开头定义变量
		// 例如：private var _example:String;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		protected var drawLayer:Sprite;
		protected var graph:Graph;
		protected var decorator:IDecorator;
		protected var drawRect:IDraw;
		
		private var _rect:Rectangle;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 公有变量声明处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		protected var _showInteLine:Boolean;
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 构造函数，初始化相关工作可以放在里面
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		/**
		 * DecoratorManager，构造函数。
		 * @param graph
		 * @param drawLayer
		 * 
		 */
		public function DecoratorManager(graph:Graph, drawLayer:Sprite)
		{
			this.drawLayer = drawLayer;
			this.graph = graph;
			
			showInteLine = true;
			
			_rect = new Rectangle();
			graph.addEventListener(MouseEvent.MOUSE_DOWN, onGraphMouseDown);
		}
		//构造函数结束
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// getter和setter函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		/**
		 * 是否显示智能参考线； 
		 * @return 
		 * 
		 */
		public function get showInteLine():Boolean
		{
			return _showInteLine;
		}
		
		public function set showInteLine(value:Boolean):void
		{
			if( _showInteLine !== value)
			{
				_showInteLine = value;
			}
			
			if(value){
				graph.addEventListener(GraphEvent.NODE_MOVING,onPositionChanged);
				graph.addEventListener(GraphEvent.NODE_MOVED,onNodeMoved);
			}else{
				graph.removeEventListener(GraphEvent.NODE_MOVING,onPositionChanged);
				graph.removeEventListener(GraphEvent.NODE_MOVED,onNodeMoved);
			}
		}
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 相关事件响应函数和逻辑函数存放处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		/**
		 * 节点移除以后，清除智能参考线，调整位置，使其与其它节点进行对齐； 
		 * @param event
		 * 
		 */
		protected function onNodeMoved(event:Event):void
		{
			decorator.cleanup(drawLayer);
			decorator.adjustPos(graph.getAllNodes(), graph.getSelectedObjects());
		}
		
		/**
		 * 位置更改的时候，清除线； 
		 * @param event
		 * 
		 */
		protected function onPositionChanged(event:GraphEvent):void
		{
			var inGraph:Boolean;
			for (var i:int = 0; i < graph.getAllNodes().length; i++) 
			{
				if(graph.getAllNodes()[i] == graph.getSelectedObjects()[0]){
					inGraph = true;
					break;
				}
			}
			
			if(inGraph){
				decorator.updatePos( graph.getAllNodes(), graph.getSelectedObjects(), drawLayer);
			}else{
				decorator.cleanup(drawLayer);	
			}
			
		}
		
		/**
		 * 添加自动对齐边饰； 
		 * @param decorator
		 * 
		 */
		public function addDecorator( decorator:IDecorator ) : void
		{
			this.decorator = decorator;		
		}
		
		/**
		 * 添加矩形框边饰器； 
		 * @param decorator
		 * 
		 */
		public function addBackGroundRectDecorator( decorator:IDraw ) : void
		{
			this.drawRect = decorator;		
		}
		
		protected function onGraphMouseDown(event:MouseEvent):void
		{
			_rect.x = drawLayer.mouseX;
			_rect.y = drawLayer.mouseY;
			
			drawLayer.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			
			//			drawLayer.addEventListener(MouseEvent.MOUSE_UP, graphMouseUpHandler);
			graph.stage.addEventListener(MouseEvent.MOUSE_UP, graphMouseUpHandler);
		}
		
		/**
		 * 绘制矩形框； 
		 * @param event
		 * 
		 */
		protected function enterFrameHandler(event:Event):void
		{
			_rect.width = drawLayer.mouseX - _rect.x;
			_rect.height = drawLayer.mouseY - _rect.y;
			
			drawRect.drawRect(_rect,drawLayer);
		}
		
		/**
		 * 鼠标弹起清除绘制层画笔，设置焦点； 
		 * @param event
		 * 
		 */
		protected function graphMouseUpHandler(event:Event):void {
			drawLayer.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			
			drawLayer.removeEventListener(MouseEvent.MOUSE_UP, graphMouseUpHandler);
			graph.stage.removeEventListener(MouseEvent.MOUSE_UP, graphMouseUpHandler);
			
			drawRect.cleanup(drawLayer)					//清除矩形框；
			//--获得框选矩形框；
			
			if (_rect.width > 0 && _rect.height > 0){
				
			}else if (_rect.width < 0 && _rect.height < 0){
				_rect.x = drawLayer.mouseX;
				_rect.y = drawLayer.mouseY;
				_rect.height *= -1;
				_rect.width *= -1;
			}else if (_rect.width > 0 && _rect.height < 0){
				_rect.y -= _rect.height * (-1);
				_rect.height *= -1; 
			}else{
				_rect.x -= _rect.width * (-1);
				_rect.width *= -1;
			}
			graph.setNodesSelected(_rect);
			graph.setFocus();
		}
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// override 覆盖函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
	}//类结束
}//包结束